package com.myapplication.ui.main;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.myapplication.R;
import com.myapplication.ui.homefragment.Fragment_Home;
import com.myapplication.ui.infofragment.Fragment_Info;
import com.myapplication.ui.listfragment.Fragment_List;
import com.myapplication.ui.showfragment.Fragment_Show;

import cn.bmob.v3.Bmob;


public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    ImageButton imageHome;
    TextView tvHome;
    LinearLayout tabHome;
    ImageButton imageInfo;
    TextView tvInfo;
    LinearLayout tabInfo;
    ImageButton imageShow;
    TextView tvShow;
    LinearLayout tabShow;
    ImageButton imageList;
    TextView tvList;
    LinearLayout tabList;

    private  Fragment home;
    private  Fragment info;
    private  Fragment show;
    private  Fragment list;

    private FragmentTransaction transaction;
    private FragmentManager manager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bmob.initialize(this, "42ea9469e2cbaf8d996b18eef5d50547");  //初始化Bomby云
        setContentView(R.layout.activity_main);
        Log.i("TAG", "onCreate: ");

        manager = getSupportFragmentManager();
        initView();
        tabHome.setOnClickListener(this);
        tabInfo.setOnClickListener(this);
        tabShow.setOnClickListener(this);
        tabList.setOnClickListener(this);


        setView();
    }

    private void setView() {
        transaction = manager.beginTransaction();
        hideFragmnet();
        setTab(0);
        if (home == null) {
            home = new Fragment_Home();
            transaction.add(R.id.main_fragment, home);
        } else {
            transaction.show(home);
        }
        transaction.commit();
    }

    private void initView() {
        imageHome= (ImageButton) findViewById(R.id.image_home);
        tvHome= (TextView) findViewById(R.id.tv_home);
        imageInfo= (ImageButton) findViewById(R.id.image_info);
        tvInfo= (TextView) findViewById(R.id.tv_info);
        imageShow= (ImageButton) findViewById(R.id.image_show);
        tvShow= (TextView) findViewById(R.id.tv_show);
        imageList= (ImageButton) findViewById(R.id.image_list);
        tvList= (TextView) findViewById(R.id.tv_list);

        tabHome= (LinearLayout) findViewById(R.id.tab_home);
        tabInfo= (LinearLayout) findViewById(R.id.tab_info);
        tabShow= (LinearLayout) findViewById(R.id.tab_show);
        tabList= (LinearLayout) findViewById(R.id.tab_list);
    }


    public void setTab(int i) {
        resetImg();
        switch (i) {
            case 0:
                imageHome.setImageResource(R.mipmap.btn_home_pressed);
                tvHome.setTextColor(getResources().getColor(R.color.colorRed));
                break;
            case 1:
                imageInfo.setImageResource(R.mipmap.btn_information_pressed);
                tvInfo.setTextColor(getResources().getColor(R.color.colorRed));
                break;
            case 2:
                imageShow.setImageResource(R.mipmap.btn_show_pressed);
                tvShow.setTextColor(getResources().getColor(R.color.colorRed));
                break;
            case 3:
                imageList.setImageResource(R.mipmap.btn_list_pressed);
                tvList.setTextColor(getResources().getColor(R.color.colorRed));
                break;
        }
    }

    private void resetImg() {
        imageList.setImageResource(R.mipmap.btn_list_normal);
        tvList.setTextColor(getResources().getColor(R.color.colorBlack));

        imageShow.setImageResource(R.mipmap.btn_show_normal);
        tvShow.setTextColor(getResources().getColor(R.color.colorBlack));

        imageInfo.setImageResource(R.mipmap.btn_information_normal);
        tvInfo.setTextColor(getResources().getColor(R.color.colorBlack));

        imageHome.setImageResource(R.mipmap.btn_home_normal);
        tvHome.setTextColor(getResources().getColor(R.color.colorBlack));

    }
    public void onClick(View view) {
        transaction = manager.beginTransaction();
        hideFragmnet();
        switch (view.getId()) {
            case R.id.tab_home:
                setTab(0);
                if (home == null) {
                    home = new Fragment_Home();
                    transaction.add(R.id.main_fragment, home);
                } else {
                    transaction.show(home);
                }
                break;
            case R.id.tab_info:
                setTab(1);
                if (info == null) {
                    info = new Fragment_Info();
                    transaction.add(R.id.main_fragment, info);
                } else {
                    transaction.show(info);
                }

                break;
            case R.id.tab_show:
                setTab(2);
                if (show == null) {
                    show = new Fragment_Show();
                    transaction.add(R.id.main_fragment, show);
                } else {
                    transaction.show(show);
                }
                break;
            case R.id.tab_list:
                setTab(3);
                if (list == null) {
                    list = new Fragment_List();
                    transaction.add(R.id.main_fragment, list);
                } else {
                    transaction.show(list);
                }
                break;
        }
        transaction.commit();
    }

    /**
     * 隐藏所有已加载Fragment的方法
     */
    private void hideFragmnet() {
        if (home != null) {
            transaction.hide(home);
        }
        if (info != null) {
            transaction.hide(info);
        }
        if (show != null) {
            transaction.hide(show);
        }
        if (list != null) {
            transaction.hide(list);
        }

    }
}
